package edu.mta.pokerstats;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.cloudfoundry.services.CloudFoundryServices;
import org.cloudfoundry.services.ServiceManager;

public class Game {
	private int m_Id;
	private int m_TeamId;
	private Date m_Date;
	private int m_CostOfBuyIn;
	private int m_CostOfRebuy;
	private String m_Remark;

	public Game(int i_Id, int i_TeamId, Date i_Date,int i_CostOfBuyIn,
				int i_CostOfRebuy, String i_Remark) {
		m_Id = i_Id;
		m_TeamId = i_TeamId;
		m_Date = i_Date;
		m_CostOfBuyIn = i_CostOfBuyIn;
		m_CostOfRebuy = i_CostOfRebuy;
		m_Remark = i_Remark;
	}

	public Game(ResultSet i_RS) {
		try {
			m_Id = i_RS.getInt("id");
			m_TeamId = i_RS.getInt("teamID");
			m_Date = i_RS.getDate("date");
			m_CostOfBuyIn = i_RS.getInt("costOfBuyIn");
			m_CostOfRebuy = i_RS.getInt("costOfRebuy");
			m_Remark = i_RS.getString("remark");
		} catch (SQLException e) {
			new Game(0, 0, new Date(0), 0, 0, "Invalid");
		}
	}

	public int getId() {
		return m_Id;
	}

	public void setId(int i_Id) {
		this.m_Id = i_Id;
	}

	public int getTeamId() {
		return m_Id;
	}

	public void setTeamId(int i_TeamId) {
		this.m_Id = i_TeamId;
	}

	public Date getDate() {
		return m_Date;
	}

	public void setDate(Date m_Date) {
		this.m_Date = m_Date;
	}

	public int getCostOfBuyIn() {
		return m_CostOfBuyIn;
	}

	public void setCostOfBuyIn(int m_CostOfBuyIn) {
		this.m_CostOfBuyIn = m_CostOfBuyIn;
	}

	public int getCostOfRebuy() {
		return m_CostOfRebuy;
	}

	public void setCostOfRebuy(int m_CostOfRebuy) {
		this.m_CostOfRebuy = m_CostOfRebuy;
	}

	public String getRemark() {
		return m_Remark;
	}

	public void setRemark(String m_Remark) {
		this.m_Remark = m_Remark;
	}

	public ArrayList<GameRecord> GetRecordsForGame() {
		ArrayList<GameRecord> recordsList = new ArrayList<GameRecord>();
		Connection connection = null;
		try {
			connection = (Connection) ServiceManager.INSTANCE
					.getInstance(CloudFoundryServices.MYSQL);
			Statement s = connection.createStatement();
			String query = "Select * from gameRecords where gameID=" + m_Id
					+ " order by position asc;";
			ResultSet rs = s.executeQuery(query);
			while (rs.next()) {
				GameRecord tempRecord = new GameRecord(rs);
				if (tempRecord != null) {
					recordsList.add(tempRecord);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					if (!connection.isClosed()) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return recordsList;
	}

	public boolean InsertToDatabase() {
		int newId;
		boolean success = false;
		newId = ServiceManager.INSTANCE.InsertQuery(getInsertString());
		if (newId >= 0) {
			m_Id = newId;
			success = true;
		}
		return success;
	}

	public boolean UpdateInDatabase() {
		return ServiceManager.INSTANCE.UpdateQuery(getUpdateString());
	}

	public boolean DeleteFromDatabase() {
		return ServiceManager.INSTANCE.DeleteFromDatabase("games", m_Id);
	}

	private String getInsertString() {
		String result = "insert into games values(" + null + "," + m_TeamId
				+ "," + "'" + m_Date + "'," + m_CostOfBuyIn + ","
				+ m_CostOfRebuy + "," + "'" + m_Remark + "')";

		return result;
	}

	public int GetInvestment(int i_NumOfRebuys) {
		int moneyInvested = m_CostOfBuyIn;
		moneyInvested += (i_NumOfRebuys * m_CostOfRebuy);
		return moneyInvested;
	}

	private String getUpdateString() {
		String result = "update games SET " + "teamID = " + m_TeamId + ","
				+ "date = '" + m_Date + "'," + "costOfBuyIn = " + m_CostOfBuyIn + ","
				+ "costOfRebuy = " + m_CostOfRebuy + "," + "remark = '"
				+ m_Remark + "' WHERE id=" + m_Id;

		return result;
	}

	public static Game getGameById(int i_Id) {
		Game returnValue = null;
		Connection connection = null;
		try {
			connection = (Connection) ServiceManager.INSTANCE
					.getInstance(CloudFoundryServices.MYSQL);
			Statement s = connection.createStatement();
			String query = "Select * from games where id =" + i_Id;
			ResultSet rs = s.executeQuery(query);
			rs.first();
			returnValue = new Game(rs);
		} catch (Exception e) {
			returnValue = null;
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					if (!connection.isClosed()) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		return returnValue;
	}
}